home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
DIUPDATE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-16
|
4KB
|
136 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: diupdate.c
// Title: Data File I/O Library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains code to update a logical file within a physical file.
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <di.h>
//----------------------------------------------------------------------------
// Description: Update the data area of a logical file.
// Parameters: pcsz Physical file name.
// pcszLogical Logical file to update
// usType Logical file type
// pcszFile File name to append. The file must be able to
// fit within the area allocated to the previous
// file.
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
BOOL FN_E DioUpdate(PCSZ pcsz, PCSZ pcszLogical, USHORT usType, PCSZ pcszFile)
{
HF hf1;
HF hf2 = -1;
HPF hpf;
HLF hlf = -1;
SIZET cDir;
DATADIR dir;
FLAG16 fs = FL_OPEN|FL_DENYREADWRITE|FL_BINARY;
FPOS fsize, flen;
BOOL fUser;
BOOL fResult = FALSE;
if (HIUSHORT(pcsz)) // Open physical file
{
if (!DioOpenPhysical(pcsz, &hpf, TRUE))
return FALSE;
fUser = FALSE;
}
else
{
hpf = LOUSHORT(pcsz);
Assert(hpf >= 0 && hpf < MAX_PHYSICAL_FILES);
Assert(di.physical[hpf].fUsed);
fUser = TRUE;
}
// Not open logical file
if (!DioOpenLogical(pcszLogical, &hlf, usType))
goto ERROR_EXIT;
cDir = di.logical[hlf].cDir;
hf1 = di.logical[hlf].hf;
flen = di.logical[hlf].flen;
if ((flen % (FPOS)sizeof(DATAHDR)) != 0)
{
flen += sizeof(DATAHDR); // Round up to nearest multiple of
flen /= sizeof(DATAHDR); // header size
flen *= sizeof(DATAHDR);
}
// Open external file
if (!FileOpen(&hf2, pcszFile, fs, NULL))
return FALSE;
fsize = FileGetSize(hf2); // Get size of external file
if (!FileSetPos(hf2, 0, FL_BEGIN))
goto ERROR_EXIT;
if (fsize > flen) // Check that new data will fit
{
Error("Updated data file is too large.");
goto ERROR_EXIT;
} // Copy data
if (!FileCopy(hf1, di.logical[hlf].fbase, hf2, 0, fsize))
goto ERROR_EXIT;
// Read directory
if (!DioDirRead(hpf, cDir, &dir))
goto ERROR_EXIT;
dir.flen = fsize;
// Calculate crc
if (!FileCrc(hf1, &dir.crcData, dir.flen, dir.fbase))
goto ERROR_EXIT;
// Write updated header
if (!DioDirWrite(hpf, cDir, &dir))
goto ERROR_EXIT;
fResult = TRUE;
ERROR_EXIT:
if (hf2 >= 0)
FileClose(hlf);
if (hlf >= 0)
DioCloseLogical(hlf);
if (!fUser)
DioClosePhysical(hpf);
return fResult;
}
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------